<!DOCTYPE html>
<html>
<head>
    <title>攻略系列</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategyCatalogs.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/common.js"></script>

    <script>
        $(function () {
            var params = getParams();

            if (params.id){
                //根据大攻略id查询大攻略及攻略分类
                $.get("/strategies/"+params.id+"/catalogs",function (data) {
                    console.log(data);
                    var index = 1;
                    $('#followNum').html(data.fabulousNum);
                    $('#collectNum').html(data.collectNum);
                    $('.strategies .title').html(data.title);
                    $('.catalogs').renderValues(data,{
                        getHtml:function (item, value) {
                            $(item).html(index+"/"+value);
                            index++;
                        },
                        getHref:getHref
                    });
                });

                /*//根据大攻略id查询攻略评论
                $.get("/strategyComments/"+params.id,{pageSize:3},function (data) {

                    console.log(data);
                });*/

                //当前页
                var currentPage = 1;

                //最大页数
                var pages;

                //合并游记数组
                var travelArr = [];

                query();

                $(window).scroll(function () {
                    if ($(document).scrollTop()+$(window).height()>=$(document).height()){
                        if (currentPage<=pages){
                            query();
                        }else {
                            $(document).dialog({
                                type : 'notice',
                                infoText: '已经到达底部了',
                                autoClose: 2500,
                                position: 'bottom'  // center: 居中; bottom: 底部
                            });
                        }
                    }
                });

                function query() {
                    $.get("/strategyComments",{currentPage:currentPage,strategyId:params.id},function (data) {
                        //合并数组
                        $.merge(travelArr,data.list);
                        console.log(travelArr);
                        $('#comment').renderValues({list:travelArr},{
                            getStar:function (item, value) {
                                var str="";
                                for (var i = 0;i < value;i ++){
                                    str += '<i class="fa fa-star"></i>';
                                }

                                for (var i = 0;i < 5-value; i++){
                                    str +='<i class="fa fa-star-o"></i>'
                                }
                                $(item).html(str);
                            },
                            getHref:getHref
                        });
                        pages=data.pages;
                    });
                    currentPage = currentPage + 1;
                }


                //点评攻略
                $('#commentBtn').click(function () {
                    window.location.href="/mine/addComment.html?id="+params.id;
                });


                var user = sessionStorage.getItem("user");

                if (user){
                    //查询当前用户有无点赞当前攻略
                    $.get("/users/fabulousStrategy/"+params.id,function (data) {
                        if(data.result){
                            $('#spanText').html("取消点赞");
                        }else {
                            $('#spanText').html("点赞");
                        }
                    });

                    //查看当前用户是否收藏当前攻略
                    $.get("/users/collectStrategy/"+params.id,function(data){
                        if(data.result){
                            $('#collectBtn').html("取消收藏")
                        }else{
                            $('#collectBtn').html("收藏")
                        }
                    });
                }

                //攻略点赞
                $('.focus .fa-thumbs-up').click(function () {
                    //判断是否有用户登录
                    if (user){
                        //判断是点赞还是取消点赞
                        if($('#spanText').html()=="点赞"){
                            $.ajax({
                                url:"/strategies/"+params.id+"/fabulous",
                                type:"post",
                                success:function (data) {
                                    $('.focus .fa-thumbs-up').html("<span>"+data.result+"</span><span id='spanText'>取消点赞</span>");
                                }
                            })
                        }else{
                            $.ajax({
                                url:"/strategies/"+params.id+"/disFabulous",
                                type:"delete",
                                success:function (data) {
                                    $('.focus .fa-thumbs-up').html("<span>"+data.result+"</span><span id='spanText'>点赞</span>");
                                }
                            })
                        }
                    }else{
                        window.location.href="/login.html";
                    }
                });

                //攻略收藏
                $('#collectBtn').click(function () {
                    //判断用户是否登录
                    if(user){
                        if ($(this).html()=="收藏"){
                            //收藏
                            $.ajax({
                                url:"/strategies/collect/"+params.id,
                                type:"patch",
                                success:function(data){
                                    $('#collectNum').html(data.result);
                                    $('#collectBtn').html("取消收藏");
                                }
                            });
                        }else {
                            //取消收藏
                            $.ajax({
                                url:"/strategies/disCollect/"+params.id,
                                type:"patch",
                                success:function(data){
                                    $('#collectNum').html(data.result);
                                    $('#collectBtn').html("收藏");
                                }
                            });
                        }
                    }else {
                        window.location.href="/login.html";
                    }
                });
            }

        })
    </script>


</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control searchBtn" placeholder="搜索旅行攻略">
            </div>
        </div>
    </div>
</div>

<div class="container strategies">
    <h5 class="title">广州攻略</h5>
    <small><span>0</span>人阅读过此攻略</small>
    <div class="focus">
        <i class="fa fa-heart"><span id="collectNum"></span><span id="collectBtn">收藏</span></i>
        <i class="fa fa-thumbs-up"><span id="followNum"></span><span id="spanText">点赞</span></i>
        <button class="btn">想去</button>
        <button class="btn">去过</button>
    </div>
</div>
<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab">
    <li class="nav-item ">
        <a class="nav-link active" id="pills-catalog-tab" data-toggle="pill" href="#pills-catalog">
            <span>攻略目录</span>
        </a>
    </li>
    <li class="nav-item" id="strategyCommend">
        <a class="nav-link " id="pills-comment-tab" data-toggle="pill" href="#pills-comment">
            <span>骡友点评</span>
        </a>
    </li>
</ul>
<div class="tab-content " id="pills-tabContent">
    <div class="tab-pane fade show active" id="pills-catalog">
        <div class="container hot">
            <h6>大家都在看</h6>
            <div>
                <a href="travelContent.html">
                    <button class="btn">上下九</button>
                </a><a href="travelContent.html">
                <button class="btn">入住长隆熊猫酒店，天天都是儿童节</button>
            </a><a href="travelContent.html">
                <button class="btn">飞到羊城，只为一场最地道的早茶之旅</button>
            </a></div>
        </div>
        <div class="catalogs">
            <div render-loop="catalogs">
                <div class="container detail">
                    <h6 render-fun="getHtml" render-key="catalogs.name"></h6>
                    <div render-loop="catalogs.details">
                        <a render-fun="getHref" render-key="catalogs.details.id" data-href="/strategyDetails.html?id=">
                            <button class="btn" render-html="catalogs.details.title"></button>
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="tab-pane fade " id="pills-comment">

        <div class="container tag">
            <h6>骡友标签</h6>
            <div>
                <span>价格便宜</span>
                <span>很热闹</span>
                <span>交通方便</span>
                <span>吃的很多</span>
                <span>风景优美</span>
                <span>美食很多</span>
                <span>很特别</span>
                <span>爱上这座城</span>
                <span>住宿方便</span>
                <span>刚刚</span>
            </div>
        </div>
        <hr>

        <div id="comment">
            <div render-loop="list">
                <div class="container row comment">
                    <div class="col-2 comment-head">
                        <a data-href="/userProfiles.html?id=" render-fun="getHref" render-key="list.user.id">
                            <img class="rounded-circle" render-src="list.user.headImgUrl">
                        </a>
                    </div>
                    <div class="col comment-right">
                        <p class="authorName"><span render-html="list.user.nickName"></span></p>
                        <span class="comment-star" render-key="list.starNum" render-fun="getStar">
                        </span>
                        <span class="comment-date" render-html="list.createTime"></span>
                        <div class="comment-content">
                            <a data-href="/strategyComment.html?id=" render-fun="getHref" render-key="list.id">
                                <p render-html="list.content"></p>
                            </a>
                        </div>

                        <ul class="comment-img" render-loop="list.imgArr">
                            <li><img render-src="list.imgArr.self"></li> 
                        </ul>

                        <div class="d-flex icon">
                            <a  data-href="/strategyComment.html?id=" render-fun="getHref" render-key="list.id">
                                <i class="fa  fa-commenting-o " style="margin-right: 10px"> 评论 </i>
                                <i class="fa fa-thumbs-o-up" > <span >0</span> </i>
                            </a>
                        </div>
                    </div>
                </div>
                </div>
            </div>
        </div>

    </div>
</div>

<div class="operation">
    <button class="btn">加入旅行单</button>
    <button class="btn" id="commentBtn">点评</button>
</div>


</body>
</html>